home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / C_ALGORI.ZIP / CPP_OVER.TXT < prev    next >
Encoding:
Text File  |  1992-05-12  |  10.5 KB  |  423 lines

  1.  
  2.  
  3.  
  4.             C++ Overview
  5.  
  6.  
  7.             by John Tal
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. 1.0    C++ and C
  17.  
  18. C++ is an object-oriented extension to the C language.  C++ can be
  19. used to compile C programs.  Everything you can do in C, you can also
  20. do in C++.  C++ programs and their extensions cannot be compiled under 
  21. a C only compiler.
  22.  
  23. 2.0    Object-Oriented Programming
  24.  
  25. 2.1    Class
  26.  
  27. The class concept is the fundamental building block for all
  28. objected-oriented programming systems (OOPS).  A class consists of an
  29. object (or group of objects) and the function(s) which operate on the
  30. obects(s).  In C++, a class is defined in a similar way to a
  31. structure, except the keyword class is used:
  32.  
  33.         class FILE_C;     // forward reference to a class
  34.  
  35.  
  36. 2.2    Class Components
  37.  
  38. A class definition contains declarations for variables and functions. 
  39. A class definition also provides different security areas for
  40. variables and functions.
  41.  
  42.         class FILE_C
  43.         {
  44.           private:
  45.               long    ptr;          // file pointer
  46.             char    name[FNAME_LEN];  // file name
  47.             short    state;          // file state
  48.             short    mode;          // file mode
  49.           public:
  50.             FILE_C(void);        // class constructor
  51.             ~FILE_C(void);            // class destructor
  52.             short    Open(char *,short);  // open function
  53.                   short    Close(void);         // close function
  54.             short    Read(char *,short);  // read function
  55.             short    Write(char *,short); // write function
  56.         };
  57.  
  58. The above FILE_C class has four private data items (objects) and six
  59. functions which operate on those objects.  Any access to the class
  60. objects must occur through a class function.  The private keyword
  61. enforces this data hiding by preventing the application using the
  62. class from having access to anything in the class private area.  Only
  63. the class functions themselves can access private objects.
  64.  
  65. 2.3    Class Constructors And Destructors
  66.  
  67. The FILE_C class has a FILE_C() function as a constructor and a
  68. ~FILE_C() function as a destructor.  The constructor is a function
  69. with the same name as the class which is invoked at the creation of
  70. an instance of the class and is intended to provide any
  71. initialization the class requires.   The destructor function also 
  72. has the same name as the class but with a '~' (tilde) character in 
  73. front of it.  A destructor is provided for any cleanup work to occur 
  74. within the class at the termination of the class instance (such as 
  75. making sure the file is closed).
  76.  
  77. A class instance is created in two different ways.  The first is by
  78. declaring a class instance in the same way a standard C language
  79. variable is created.
  80.  
  81.     short    x;         //  create a variable named x
  82.     FILE_C  LogFile;   // create a unique instance of the FILE_C class
  83.  
  84.  
  85. The second method of creating a class instance is by allocating a new
  86. instance through a pointer and the new keyword.
  87.  
  88.     FILE_C * pLogFile;    // create a pointer to a FILE_C class
  89.  
  90.     pLogFile = new FILE_C;    // create an instance and assign ptr
  91.  
  92. The new keyword is provided as an improved calloc or malloc.  The new
  93. keyword calculates the size of the memory block to be allocated to
  94. the item being created.
  95.  
  96. 2.3.1    Constructors With Parameters
  97.  
  98. Like any function, a constructor can take parameters.   The
  99. parameters would be supplied at the time of the creation of a new
  100. class instance.
  101.  
  102.     class FILE_C
  103.     {
  104.         ...
  105.       public:
  106.         FILE_C(char *pFileName);
  107.     };
  108.  
  109.     FILE_C    LogFile("LogFileName");  // constructor with parm
  110.  
  111.  
  112. 2.4    Class Function Calling
  113.  
  114. Class member functions are called like normal C functions.  The
  115. difference is that they use syntax similar to that used for structure
  116. membership.
  117.  
  118.     
  119.     FILE_C    LogFile;    // create instance of FILE_C
  120.     FILE_C * pInputFile;    // create ptr to instance of FILE_C
  121.  
  122.     pInputFile = new FILE_C;   // create instance of FILE_C
  123.  
  124.     LogFile.Open("LogFile",O_APPEND);    // open LogFile
  125.  
  126.     InputFile -> Open("InputDat",O_READONLY);  // open InputFile
  127.  
  128.     InputFile -> Read(buffer,sizeof(buffer));  // read InputFile
  129.  
  130.  
  131. From the above example, a file pointer is never sent to the FILE_C
  132. functions as would be in standard C file functions.  This is because
  133. each instance of the FILE_C maintains its own control information
  134. internal to the class itself in its own private area.  C++ usually
  135. simplifies interfaces between classes and applications because
  136. classes are complete in themselves.  They contain all the attributes
  137. and/or objects that describe the class within.
  138.  
  139.  
  140. 2.5    Class Function Declaration
  141.  
  142. A class definition (such as class FILE_C ...) would occur in an 
  143. include file.  The actual functions of the class would be declared in
  144. a C++ source file.   Each class function is prefixed with the class
  145. name to which it belongs and the symbol '::'.
  146.  
  147.     short FILE_C::Open(char * pFileName,short mode)
  148.     {
  149.         mode = mode;   // referencing private data item
  150.  
  151.         strcpy(name,pFileName);
  152.  
  153.         //  perform open
  154.  
  155.          return (status);
  156.     }
  157.  
  158.  
  159. 2.6    Inline Functions
  160.  
  161. If a class function is performing a very simple task, it can be
  162. declared an an inline function.  An inline function is an expanded
  163. version of the function declaration within the class with begin and
  164. end braces surronding the inline statement(s).
  165.  
  166.     class FILE_C
  167.     {
  168.       private:
  169.         char    name[FNAME_LEN];  // file name
  170.         ...
  171.       public:
  172.          FILE_C(char *pFileName) { strcpy(name,pFileName); }
  173.         ...
  174.     };
  175.  
  176.  
  177. The above example shows the FILE_C constructor implemented as an
  178. inline function.  Inline functions should be limited to functions
  179. having only a few (preferrably one) statement(s).
  180.  
  181.  
  182. 3.0    Derived Classes
  183.  
  184. One of C++' most powerful features is to use classes as building
  185. blocks in creating entirely new classes.
  186.  
  187.     class BROWSE_C : FILE_C   // browse derived from file
  188.     {          
  189.       private:
  190.         short  curline;
  191.         ...
  192.       public:
  193.         BROWSE_C(void);
  194.         ~BROWSE_C(void);
  195.         OpenFile(char *);
  196.     };
  197.  
  198.  
  199. From the above example, the BROWSE_C class will have access not only
  200. to all of its own member data/objects, but also to all FILE_C class
  201. member functions which were declared as public or protected in
  202. FILE_C.  The following table breaks down class security areas for
  203. immediate classes and derived classes.
  204.  
  205.  
  206.     Immediate        Derived
  207.     ----------        ---------
  208.     Private            Not-visible in derived class
  209.     Protected        Visible as Private in derived
  210.     Public            Visible as Protected in derived
  211.  
  212. From the above example, the BROWSE_C class would be able to access
  213. any data and functions which were defined as protected or public in
  214. the FILE_C class.  The application would not be able to access any of
  215. the data or functions of the FILE_C class without going through a
  216. public member function of the BROWSE_C class.   These are the default
  217. security inheritance protocols for classes.
  218.  
  219. 3.1    Customizing Class Inheritance
  220.  
  221. The default security inheritance can be overridden when defining
  222. the derived class:
  223.  
  224.     class BROWSE_C : public FILE_C   // browse derived from file
  225.     {          
  226.       private:
  227.         short  curline;
  228.         ...
  229.       public:
  230.         BROWSE_C(void);
  231.         ~BROWSE_C(void);
  232.         OpenFile(char *);
  233.                            
  234.     };
  235.  
  236. From the above example, all public functions of FILE_C class are also
  237. public to applications using the BROWSE_C class.
  238.     
  239.  
  240. 3.2    Container Classes
  241.  
  242. Container classes are classes which contain other classes.  An example
  243. would be a class to implement a binary tree:
  244.  
  245.     class TREE_C
  246.     {
  247.  
  248.        private:
  249.  
  250.         struct TNODE_S     // the contained class
  251.         {
  252.           PVOID          pvData;
  253.           struct TNODE_S *pstLeft;
  254.           struct TNODE_S *pstRight;
  255.         };
  256.         typedef struct TNODE_S TNODE_T;
  257.         typedef TNODE_T *TNODE_P;
  258.         typedef TNODE_T **TNODE_PP;
  259.      
  260.         TNODE_P  pstHead;
  261.         TNODE_P  pstNode;
  262.         ...
  263.     
  264.     public:
  265.     
  266.         TREE_C(VOID);
  267.         ~TREE_C(VOID);
  268.         SHORT    Delete(PVOID);          // Remove entry
  269.         SHORT    Find(PVOID,PPVOID);     // Find entry
  270.         SHORT    Insert(PVOID);          // Insert entry
  271.         ...
  272.     };
  273.     
  274.     typedef TREE_C * TREE_CP;
  275.     typedef TREE_C ** TREE_CPP;
  276.  
  277. In the binary tree example, it was not desirable for each node of the
  278. tree to be an instance of the TREE_C class.  So each node is
  279. contained in the TREE_C class and the TREE_C class operates on all
  280. the TNODE_S structures/classes contained within it.
  281.  
  282. 3.3    Virtual Functions
  283.  
  284. Virtual functions provide a way for a base class function to take on
  285. the characteristics or behavior appropriate to the current derived
  286. class.
  287.  
  288.     class FILE_C
  289.     {
  290.       private:
  291.         char    name[FNAME_LEN];  // file name
  292.         ...
  293.       public:
  294.          FILE_C(char *pFileName) { strcpy(name,pFileName); }
  295.          virtual short Reset(void);  
  296.     };
  297.  
  298.     class BROWSE_C : FILE_C   // browse derived from file
  299.     {          
  300.       private:
  301.         short  curline;
  302.         ...
  303.       public:
  304.         BROWSE_C(void);
  305.         ~BROWSE_C(void);
  306.         OpenFile(char *);
  307.         short Reset(void);
  308.     };
  309.  
  310.     short BROWSE_C::Reset(void)
  311.     {
  312.         FILE_C::Reset();
  313.         curline = 0;
  314.         }
  315.  
  316.  
  317.  
  318.  
  319. 4.0    Operator Overloading
  320.  
  321. Since C++ classes define in detail the characters of and operations
  322. upon the class instance, C++ allows all standard operators (i.e. '+',
  323. '-', '*', '/', '++', '--') to be redefined or overloaded for the
  324. current class.
  325.  
  326.     class STRING_C
  327.     {
  328.        private:
  329.          char * pChar;
  330.          int  len;
  331.         ...
  332.        public:
  333.          STRING_C(const char * = 0);  // provide default value
  334.         ~STRING_C(delete pChar);
  335.          void operator+(char *)
  336.           };
  337.  
  338.     STRING_C::operator+(char *pC)
  339.     {
  340.        char * pBuf;
  341.        pBuf = new char[len=strlen(pC)+len];
  342.            strcpy(pBuf,pChar);
  343.        strcat(pBuf,pC);
  344.               delete pChar;
  345.        pChar = pBuf;
  346.     }
  347.  
  348.  
  349.     STRING_C  Str("ABC");
  350.  
  351.     Str + "DEF";       // internal pChar now = 'ABCDEF'
  352.  
  353.     
  354. Operator overloading still involves functions to simulate the
  355. operator being overloaded.  Overloading simply provides a mechanism
  356. for abbreviating the operations.
  357.  
  358.  
  359. 5.0    C++ Input/Output
  360.  
  361. C++ output is simplified over that of C printf family functions.  C++
  362. defines the keywords cout and cin and the stdout and stdin devices. 
  363. C++ automatically formats the output based on the current variable
  364. type.
  365.  
  366.  
  367.     /*  In C */
  368.  
  369.     printf("%s = %d\n", szRate, sRate);
  370.  
  371.     //  In C++
  372.  
  373.     cout << szRate << " = " << sRate << "\n";
  374.  
  375.  
  376.     //  Another C++ alternative
  377.  
  378.     cout << form("%s = %d\n", szRate,sRate);
  379.  
  380.  
  381.     /*  In C */
  382.  
  383.     scanf("%d",&sRate);
  384.  
  385.     //  In C++
  386.  
  387.     cin >> sRate;
  388.  
  389.  
  390. 7.0    Reference Variables
  391.  
  392. C++ provides a syntax which allows programmers who are not
  393. comfortable with C pointer syntax to more easily write applications
  394. which use pointers.
  395.  
  396.  
  397.     /*   In C */
  398.  
  399.     short Afunc(short * psShort)
  400.     {
  401.        *psShort++;
  402.     }
  403.  
  404.  
  405.     //   In C++
  406.  
  407.     short Afunc(short & Short)
  408.     {
  409.        Short++;
  410.     }
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.